package com.gzhryc.shared_device.oem.code.place.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.place.dao.PlaceClerkDao;
import com.gzhryc.shared_device.oem.code.place.dao.db.Clerk;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceClerk;
import com.gzhryc.shared_device.oem.code.place.dao.models.ClerkPlaceCount;
import com.gzhryc.shared_device.oem.code.place.services.dto.PlaceClerkSearch;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;

public class PlaceClerkService {

	static Logger log = Logger.getLogger(PlaceClerkService.class);

    public static PlaceClerkService self() {
        return MultiDBTools.getService(PlaceClerkService.class, true);
    }

    public static PlaceClerkService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, PlaceClerkService.class, true);
    }

    PlaceClerkDao dao;

    public PlaceClerkService(String jdbcKey) {
        this.dao = new PlaceClerkDao(jdbcKey);
    }
    
    public List<PlaceClerk> findBySearch(PlaceClerkSearch search,Integer pageNo,Integer pageLimit){
		Conditions conditions = dao.buildConditions(search);
		conditions.desc("createDate");

        try {
			return dao.findByCondition(conditions,pageNo,pageLimit);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return null;
    }
    
    public Long countBySearch(PlaceClerkSearch search){
		Conditions conditions = dao.buildConditions(search);

        try {
			return dao.count(conditions);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return null;
    }
    
    public List<PlaceClerk> findJoinByPlaceId(Long placeId){
    	if(placeId != null) {
    		Conditions conditions = new Conditions(PlaceClerk.class);
            conditions.and().is("placeId", placeId);

            try {
				return dao.findJoinByCondition(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public List<PlaceClerk> findByUserKeys(Long userId,Long merchantUserId){
    	if(userId != null && merchantUserId != null) {
    		Conditions conditions = new Conditions(PlaceClerk.class);
            conditions.and().is("merchantUserId", merchantUserId);
            conditions.and().is("userId",userId);

            try {
				return dao.findByCondition(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public List<ClerkPlaceCount> findPlaceCountByUserKeys(Long merchantUserId,List<Long> userIds){
        if(merchantUserId != null && userIds != null && userIds.size() > 0){
            Conditions conditions = new Conditions(PlaceClerk.class);
            conditions.and().is("merchantUserId", merchantUserId);
            conditions.and().in("userId",userIds);

            try {
                return dao.countGroupUserId(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }
    
    public List<ClerkPlaceCount> findPlaceCountByUserKeys(List<Long> merchantUserIds,List<Long> userIds){
        if(merchantUserIds != null && merchantUserIds != null && userIds != null && userIds.size() > 0){
            Conditions conditions = new Conditions(PlaceClerk.class);
            conditions.and().in("merchantUserId", merchantUserIds);
            conditions.and().in("userId",userIds);

            try {
                return dao.countGroupMerchantAndUserId(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }
    
    public Long countByUserKeys(Long userId,Long merchantUserId) {
    	if(userId != null && merchantUserId != null) {
    		Conditions conditions = new Conditions(PlaceClerk.class);
            conditions.and().is("merchantUserId", merchantUserId);
            conditions.and().is("userId",userId);

            try {
				return dao.count(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return 0L;
    }
    
    public PlaceClerk getByKeys(Long placeId,Long userId) {
    	if(placeId != null && userId != null) {
    		Conditions conditions = new Conditions(PlaceClerk.class);
            conditions.and().is("placeId", placeId);
            conditions.and().is("userId",userId);

            try {
				return dao.get(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public boolean add(PlaceClerk placeClerk,User user,String note) throws LogicException {
    	Clerk clerk = ClerkService.self().getJoinByKeys(user.getId(), placeClerk.getMerchantUserId());
    	if(clerk == null) {
    		clerk = new Clerk();
    		clerk.setMerchantUserId(placeClerk.getMerchantUserId());
    		clerk.setNote(note);
    		
    		placeClerk.setCreateDate(new Date());
    	
    		if(user.getId() != null) {
    			JdbcSession.begin();
	    		try {
	    			clerk.setUserId(user.getId());
	    			if(ClerkService.self().add(clerk)) {
	    				placeClerk.setUserId(clerk.getUserId());
						if(dao.insert(placeClerk) > 0) {
							JdbcSession.commit();
							return true;
						}
	    			}
	    		}catch(SQLException e) {
	    			log.error(e.getMessage(), e);
	    		}finally {
	    			JdbcSession.end();
	    		}
    		}else {
	    		JdbcSession.begin();
	    		try {
	    			if(ClerkService.self().add(clerk, user)) {
	    				placeClerk.setUserId(clerk.getUserId());
						if(dao.insert(placeClerk) > 0) {
							JdbcSession.commit();
							return true;
						}
	    			}
	    		}catch(SQLException e) {
	    			log.error(e.getMessage(), e);
	    		}finally {
	    			JdbcSession.end();
	    		}
    		}
    	}else {
    		placeClerk.setUserId(clerk.getUserId());
    		placeClerk.setCreateDate(new Date());
    		try {
				return dao.insert(placeClerk) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }


    public boolean deleteBykeys(Long placeId,Long userId) throws LogicException{
    	if(placeId != null && userId != null) {
    		Conditions conditions = new Conditions(PlaceClerk.class);
            conditions.and().is("placeId", placeId);
            conditions.and().is("userId",userId);

            try {
				return dao.deleteByCondition(conditions) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
}
